/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/11/23
 * Time: 13:37
 */
$(function(){
    $('.btn-save').on('click', function(){
        var params = {
            username: $('.user-name').val(),
            password: $('.user-pass').val()
        };
        if (checkForm(params)) {
            $.ajax({
                type: 'post',
                url: '/signIn',
                data: {params: params},
                success: function(e){
                    var data = JSON.parse(e);
                    var json = data.body.data;
                    if (json) {
                        Cookies.set('_token', json._token, {expires: 1, path: '/'});
                        var url = '';
                        if (json.role == 1) {
                            url += '/student.html';
                        }
                        if (json.role == 2) {
                            url += '/teacher.html';
                        }
                        if (json.role == 3) {
                            url += '/manager.html';
                        }
                        location.href = url;
                    } else {
                        alert('用户名或密码错误!');
                    }

                }
            });
        }
    });
});

function checkForm(params) {
    if (params.username == '') {
        alert('请输入用户名');
        return false;
    }
    if (params.password == '') {
        alert('请输入密码');
        return false;
    }
    return true;
}